<!--
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>HomePage Plugin</title>
<script language="javascript" src="../../yui/yahoo.js"></script>
<script language="javascript" src="../../yui/event.js"></script>
<script language="javascript" src="../../yui/treeview.js"></script>


<link href="../../styles/tree.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>SEMF HomePage Plugin</h1>

<?php
$root_project_home=$_GET['project_home'];
$semf_root = "../../";
$home_page = $_GET["home_page"];

$project_home = $semf_root.$root_project_home;

$conf_dir = $project_home."/store/other/plug_ins/HomePage/";
$conf_file = $conf_dir."conf.xml";

$doc = new DOMDocument();
if(isset($home_page))
{
	$root = $doc->createElement("HomePage");
	$root->setAttribute("name", $home_page);
	
	$doc->appendChild($root);
	
	if(!is_dir($project_home."/store"))
	{
		mkdir($project_home."/store");
	}
	if(!is_dir($project_home."/store/other"))
	{
		mkdir($project_home."/store/other");
	}
	if(!is_dir($project_home."/store/other/plug_ins"))
	{
		mkdir($project_home."/store/other/plug_ins");
	}
	if(!is_dir($project_home."/store/other/plug_ins/HomePage/"))
	{
		mkdir($project_home."/store/other/plug_ins/HomePage/");
	}
	$doc->save($conf_file);
}
else if(file_exists($conf_file))
{
	$doc->load($conf_file);
	
	$root = $doc->documentElement;
	$home_page = $root->getAttribute("name");
}

if(file_exists($conf_file))
{
	$xslt_file = "home_page.xsl";
	$xslt = new xsltProcessor;
	$xslt->importStyleSheet(DomDocument::load($xslt_file));
	
	$final =  $xslt->transformToDoc($doc);
	
	$target_file = "${project_home}/index.php";
	
	$index_string = $final->documentElement->nodeValue;
	
	echo $index_string;
	
	echo $index_string;
	
	$f = fopen($target_file, "w+");
	fprintf($f, $index_string);
	fclose($f);
}

$files = array();
$d = dir($project_home);
// Loop through all of the files:
$i = 0;
while (false !== ($file = $d->read()))
{
	// If the file is not this file, and does not start with a '.' or '~'
	// and does not end in LCK, then store it for later display:
	if ( ($file{0} != '.') &&
		 ($file{0} != '~') &&
		 (substr($file, -3) != 'php') &&
		 is_dir($project_home.$file) )
	{
		if($file == "store")
		{
			continue;
		}
		$files[$i++] = $file;
	}
}

$modules = array();
$pages = array();

foreach($files as $file)
{
	$module_name = $file;
	$pages[$module_name] = array();
	$modules[count($modules)] = $module_name;
	
	$module_dir = $project_home.$module_name;
	
	$d = dir($module_dir);
	// Loop through all of the files:
	$i = 0;
	while (false !== ($page = $d->read()))
	{
		// If the file is not this file, and does not start with a '.' or '~'
		// and does not end in LCK, then store it for later display:
		if(substr($page, -3) == 'xml')
		{
			if(strtolower($page) == "module.xml")
			{
				continue;
			}
			$pages[$module_name][$i++] = substr($page, 0, strlen($page)-4);
		}
	}	
}

	
?>

<script language="javascript">
function init()
{
 var confTree = new YAHOO.widget.TreeView("confTree");
 var root = confTree.getRoot();
 
 var componentHTML = "<i>Select the Home Page..</i>";

 var base = new YAHOO.widget.HTMLNode(componentHTML,
					root, true, true);
					
 <?php 
 
 	foreach($modules as $module)
	{
		$component_html = "<b>${module}</b>";
		
		echo 
			"var base_${module} = new YAHOO.widget.HTMLNode(\"${component_html}\",
					base, true, true);
			";
		$module_pages = $pages[$module];
		foreach($module_pages as $page)
		{
			$component_html = "<div>";
			$link = $semf_root."page_designer/index.php?";
			$link .= "result_dir=../".$root_project_home."/".$module."/&";
			$link .= "page_xml=../".$root_project_home."/".$module."/".$page.".xml&";
			$link .= "project_home=../".$root_project_home."/&";
			$component_html .= " <a href='${link}'>${page}</a>";
			$component_html .= "<input ";
			
			$page_name = "${module}/${page}_html.php";
			if($page_name == $home_page)
			{
				$component_html .= "checked=true ";
			}
			
			$component_html .= "page='${page_name}' onclick='change_conf(this)' type='radio' name='homePageRadio' ></input>";
			$component_html .= "</div>";
			
			echo "var componentHTML = \"${component_html}\";\n";
			
			echo "var base_${module}_${page} = new YAHOO.widget.HTMLNode(componentHTML,
					base_${module}, true, true);
					";
		}
	}
 ?>
 
 confTree.draw();
}

function change_conf(ctrl)
{
	var homePage = ctrl.getAttribute("page");
	//alert(document.forms[0].action);
	window.location.href= "index.php?home_page=" + homePage +
							"&project_home=" + "<?php echo $root_project_home; ?>";
}
</script>


<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
	<td width="106" height="19">&nbsp;</td>
    <td width="636" valign="top">
	<div id="confTree">
	</div>
	</td>
  </tr>
</table>
<script language="javascript">
init();
</script>
</body>
</html>
